package comic;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import android.graphics.drawable.Drawable;

public class QuestionableContent extends Comic {

	private static final long serialVersionUID = -4626264791440435918L;

	@Override
	public String get_display_name() {
		return "Questionable Content";
	}

	@Override
	public int get_first_comic_n() {
		return 1;
	}

	@Override
	public int getLatest() throws MalformedURLException, IOException {
		String content = get_website_content("http://questionablecontent.net/");
		String result = content.replaceAll(
				".*http://www.questionablecontent.net/comics/(\\d+).png.*",
				"$1");

		return Integer.parseInt(result);
	}

	@Override
	public Strip getStrip(int id) throws MalformedURLException, IOException {
		int latest = getLatest();
		Strip s = new Strip();

		if (id < first_comic)
			id = first_comic;
		else if (id > latest)
			id = latest;
		s.id = id;

		String url = "http://questionablecontent.net/archive.php";
		String content = get_website_content(url);

		String regex = String.format(".*Comic %d: (.*?)</a><br>.*", id);
		s.title = content.replaceAll(regex, "$1");

		URL image_url = new URL(String.format(
				"http://questionablecontent.net/comics/%d.png", id));
		s.image = Drawable.createFromStream(image_url.openStream(), "");

		s.text = "";

		return s;
	}

}
